package site.luojie.session.consistence.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author jie.luo
 * @description
 * @since 2020-06-30
 */
public class AuthInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        Object username = session.getAttribute("username");
        if (null == username) {
            logger.debug("---------> 当前为登录。请登录");
            response.sendRedirect(request.getContextPath() + "/login/toLogin");
            return false;
        }

        return true;
    }
}
